<template>
	<div class="sys-dictData-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="700px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.dictDataForm" ref="dictDataFormRef" label-width="auto">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="字典值" prop="value" :rules="[{ required: true, message: '字典值不能为空', trigger: 'blur' }]">
							<el-input v-model="state.dictDataForm.value" placeholder="字典值" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="编码" prop="code" :rules="[{ required: true, message: '编码不能为空', trigger: 'blur' }]">
							<el-input v-model="state.dictDataForm.code" placeholder="编码" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="标签类型">
							<el-radio-group v-model="state.dictDataForm.tagType">
								<el-radio value="default"><el-tag type="default">主题色</el-tag></el-radio>
								<el-radio type="success" value="success"><el-tag type="success">success</el-tag></el-radio>
								<el-radio type="info" value="info"><el-tag type="info">info</el-tag></el-radio>
								<el-radio type="warning" value="warning"><el-tag type="warning">warning</el-tag></el-radio>
								<el-radio type="danger" value="danger"><el-tag type="danger" style="font:">danger</el-tag></el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="样式(Style)" prop="styleSetting">
							<el-input v-model="state.dictDataForm.styleSetting" placeholder="样式(Style)" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="样式(Class)" prop="classSetting">
							<el-input v-model="state.dictDataForm.classSetting" placeholder="样式(Class)" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态">
							<el-radio-group v-model="state.dictDataForm.status">
								<el-radio :value="1">启用</el-radio>
								<el-radio :value="2">禁用</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序">
							<el-input-number v-model="state.dictDataForm.orderNo" placeholder="排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.dictDataForm.remark" placeholder="请输入备注内容" clearable type="textarea" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="拓展数据">
							<el-input v-model="state.dictDataForm.extData" placeholder="请输入拓展数据" clearable type="textarea" rows="6" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="sysEditDictData">
import {  reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useDictApi } from '/@/api/dict';

const props = defineProps({
	title: String,
	dictTypeId: Number,
});
const emits = defineEmits(['handleQuery', 'handleUpdate']);
const dictDataFormRef = ref();
const state = reactive({
	isShowDialog: false,
	dictDataForm: {} as DictDataForm,
});

// 打开弹窗
const openDialog = (row: any) => {
	state.dictDataForm = JSON.parse(JSON.stringify(row));
	if (JSON.stringify(row) == '{}') {
		state.dictDataForm.dictTypeId = props.dictTypeId;
	}
	state.isShowDialog = true;
	dictDataFormRef.value?.resetFields();
};

// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	dictDataFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		if (state.dictDataForm.id != undefined && state.dictDataForm.id > 0) {
			useDictApi().editDictData(state.dictDataForm).then(async (res: AjaxResult) => {
				if (res.success == true) {
					ElMessage.success('更新成功');
					emits('handleUpdate');
					closeDialog();
				}
				else {
					ElMessage.warning('更新失败：' + res.message);
				}
			});
		} else {
			useDictApi().addDictData(state.dictDataForm).then(async (res: AjaxResult) => {
				if (res.success == true) {
					ElMessage.success('新增成功');
					emits('handleUpdate');
					closeDialog();
				}
				else {
					ElMessage.warning('新增失败：' + res.message);
				}
			});
		}
		closeDialog();
	});
};

// 导出对象
defineExpose({ openDialog });
</script>
